﻿//===========================================================================================================================
// Cuberry - http://www.cuberry.net
// Copyright (c) 2012
// by Peacequare® - Amir Moussa
//===========================================================================================================================
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
// as published by the Free Software Foundation, either version 3 of the License, or any later version.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// This program is distributed in the hope that it will be useful.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//===========================================================================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

using System.Collections;


namespace P2.Cuberry.Framework.Helper
{
    public partial class P2ReportViewer : Form
    {

        public P2ReportViewer(string myLocalReportLocation, SortedList myDataSourceCollection, string reportTitle)
        {
            GetLocalReport(myLocalReportLocation, myDataSourceCollection, reportTitle);
            InitializeComponent();
        }


        public P2ReportViewer(string myRemoteReportLocation, string reportTitle)
        {
            GetRemoteReport(myRemoteReportLocation, reportTitle);
            InitializeComponent();
        }

        
        private void GetRemoteReport(string myReportLocation, string reportTitle)
        {
            ReportViewer reportViewer = new ReportViewer();
            reportViewer.LocalReport.EnableHyperlinks = true;

            // Set Processing Mode
            ////////// The Remote Version /////////////////
            reportViewer.ProcessingMode = ProcessingMode.Remote;
            reportViewer.ServerReport.ReportServerUrl = new Uri(Configuration.ConfigurationSetUp.GetAppConfigValue("ReportServer")); 
            reportViewer.ServerReport.ReportPath = myReportLocation; // @"/FiAccountingReports/ChartOfAccounts";

            //--> Add Parameters...
            
            this.Text = reportTitle;
            this.ClientSize = new System.Drawing.Size(950, 600);

            // Add the reportviewer to the form
            reportViewer.Dock = DockStyle.Fill;
            this.Controls.Add(reportViewer);

            // Process and render the report
            reportViewer.RefreshReport();
        }
        

        private void GetLocalReport(string myReportLocation, SortedList myDataSourceCollection, string reportTitle)
        {
            ReportViewer reportViewer = new ReportViewer();

            // Set Processing Mode
            ////////////// The Local Version ///////////////// 
            reportViewer.ProcessingMode = ProcessingMode.Local;
            reportViewer.LocalReport.EnableHyperlinks = true;
            
            // Get RDLC file location

            //Version 1
            reportViewer.LocalReport.ReportPath = myReportLocation;
            //Version 2
            //reportViewer.LocalReport.ReportEmbeddedResource = myReportEmbeddedResource;
            //Version 3
            //FileStream FS = File.Open(@".\Reports\PrintEn.rdlc", FileMode.Open);
            //reportViewer.LocalReport.LoadReportDefinition(FS);
            //FS.Close();


            // Supply data corresponding to each report data source.
            this.components = new System.ComponentModel.Container();


            foreach (DictionaryEntry myDataSource in myDataSourceCollection)
            {
                ReportDataSource myReportDataSource = new ReportDataSource();
                BindingSource myBindingSource = new BindingSource(this.components);
                ((System.ComponentModel.ISupportInitialize)(myBindingSource)).BeginInit();

                myReportDataSource.Name = myDataSource.Key.ToString(); //DataSource Name
                myReportDataSource.Value = myBindingSource;
                myBindingSource.DataSource = myDataSource.Value;

                reportViewer.LocalReport.DataSources.Add(myReportDataSource);
            }

            this.Text = reportTitle;
            this.ClientSize = new System.Drawing.Size(950, 600);

            // Add the reportviewer to the form
            reportViewer.Dock = DockStyle.Fill;
            this.Controls.Add(reportViewer);

            // Process and render the report
            reportViewer.RefreshReport();            
        }
        
    
    }
}
